﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Json;

namespace XingFuStudio.Pioneer.IO {
    /// <summary>
    /// Json对象处理类
    /// </summary>
    public class JsonManager {
        /// <summary>
        /// 从Json对象中获取整型值
        /// </summary>
        /// <param name="field">Json对象</param>
        /// <returns>整型值</returns>
        public static int GetInteger(JsonObject field) {
            //获取参数对应的字符串
            string value = GetString(field);

            //调用内容处理类中的获取整型数值方法, 获取字符串对应的整型
            return value.ConvertToInteger();
        }

        /// <summary>
        /// 从Json对象中获取字符串信息
        /// </summary>
        /// <param name="field">Json对象</param>
        /// <returns>字符串</returns>
        public static string GetString(JsonObject field) {
            //判断输入的参数是否为Null
            if (field == null || field.GetValue() == null) {
                //返回Null
                return null;
            }

            //声明字符串对象
            string value = null;

            //获取输入参数的数据类型
            string type = field.GetValue().GetType().Name;

            //判断类型信息, 并将该类型转换成字符串
            switch (type.ToLower()) {
                case "string":
                    value = (string)field.GetValue();
                    break;
                case "double":
                    value = field.GetValue().ToString();
                    break;
                case "boolean":
                    value = field.GetValue().ToString();
                    break;
            }

            //返回字符串
            return value;
        }
    }
}
